//
//  SelectTag_NewViewController.m
//  HAODE
//
//  Created by LiYinDi on 16/3/3.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "SelectTag_NewViewController.h"
#import "SelectTag_NewTableViewCell.h"
#import "HttpRequest.h"
#import "AddTagViewController.h"


@interface SelectTag_NewViewController ()
{
    NSMutableArray *tagNameArray;
    NSMutableArray *tagCodeArray;
    NSMutableArray *selectTagCodeArray;
    // NSMutableArray *selectTagNameArray;
//    ReabamSingleton*commdity;
}
@end

@implementation SelectTag_NewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    commdity=[ReabamSingleton shared];
    tagNameArray=[NSMutableArray array];
    tagCodeArray=[NSMutableArray array];
    selectTagCodeArray=[NSMutableArray array];
    // selectTagNameArray=[NSMutableArray array];
    [self setNavigation];
    [self setAutolayout];
    [self requestTag];
}
-(void)requestTag
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:_optionNameString forKey:@"optionName"];
    [Global showIndeterminateHud:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Common/AppCode/List" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        // NSLog(@"////===%@",severDataDict.dataDict);
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                NSMutableArray *dataArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                for (int i=0; i<dataArray.count; i++) {
                    [tagCodeArray addObject:[[dataArray objectAtIndex:i] objectForKey:@"code"]];
                    [tagNameArray addObject:[[dataArray objectAtIndex:i] objectForKey:@"content"]];
                }
                
                [_tableView reloadData];
                
                for (int i=0; i<_selectedTagCodeArray.count; i++) {
                    if ([tagCodeArray containsObject:_selectedTagCodeArray[i]]) {
                        [selectTagCodeArray addObject:_selectedTagCodeArray[i]];
                    }
                }
                
//                if (selectTagCodeArray.count>0) {
//                    [_countView setBackgroundColor:[UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.8]];
//                }
//                else
//                {
//                    [_countView setBackgroundColor:[UIColor colorWithRed:225.0f/255.0f green:225.0f/255.0f blue:225.0f/255.0f alpha:1.0]];
//                }
                
                _countLabel.text=[NSString stringWithFormat:@"已选 %lu 个",(unsigned long)selectTagCodeArray.count];
                
                //
            }else{
                [Global hideProgressHUDInternal:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    
                    //                    TheLoginViewController *detailViewController = [[TheLoginViewController alloc] init];
                    //                    [self.navigationController pushViewController:detailViewController animated:YES];
                }
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio
{
    return tagCodeArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 63;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * const  ResultTableViewCellIdentifier = @"ResultTableViewCellIdentifier";
    SelectTag_NewTableViewCell *cell=( SelectTag_NewTableViewCell *)[tableView dequeueReusableCellWithIdentifier: ResultTableViewCellIdentifier];
    if (cell==nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"SelectTag_NewTableViewCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    // [cell.checkButton setImage:[UIImage imageNamed:@"checkbox_unselected.png"] forState:UIControlStateNormal];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tagNameArray.count>0) {
        [cell.tagNameLabel setText:[tagNameArray objectAtIndex:indexPath.row]];
    }
    if ([selectTagCodeArray containsObject:[tagCodeArray objectAtIndex:indexPath.row]]) {
        [cell.checkButton setImage:[UIImage imageNamed:@"gous.png"] forState:UIControlStateNormal];
    }
    
    return cell;
    
    
}
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    return YES;
//}
//
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        [self deleteTag:[tagCodeArray objectAtIndex:indexPath.row] tagNameStr:[tagNameArray objectAtIndex:indexPath.row] index:indexPath.row];
//        
//    }
//}
//-(void)deleteTag:(NSString *)tagCodeStr tagNameStr:(NSString *)tagNameStr index:(NSInteger)index
//{
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//    NSString* Token = [user objectForKey:@"tokenId"];
//    if (Token ==nil) {
//        Token=@"dd";
//    }
//    NSMutableDictionary *param = [NSMutableDictionary new];
//    [param setValue:Token forKey:@"tokenId"];
//    [param setValue:_optionNameString forKey:@"optionName"];
//    [param setValue:tagCodeStr forKey:@"code"];
//    [param setValue:tagNameStr forKey:@"tagNameStr"];
//    [param setValue:@"D" forKey:@"editType"];
//    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
//    [HttpRequest requestData:@"Common/AppCode/Edit" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
//        if (isSuccess) {
//            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
//                [Global hideProgressHUDInternal:self.view];
//                if ([selectTagCodeArray containsObject:[tagCodeArray objectAtIndex:index]]) {
//                    [selectTagCodeArray removeObject:[tagCodeArray objectAtIndex:index]];
//                    //                            [selectTagNameArray removeObject:[tagNameArray objectAtIndex:index]];
//                }
//                
//                [tagNameArray removeObjectAtIndex:index];
//                [tagCodeArray removeObjectAtIndex:index];
//                [_tableView reloadData];
//                
//                _countLabel.text=[NSString stringWithFormat:@"已选 %lu 个",(unsigned long)selectTagCodeArray.count];
//                
//                
//            }else{
//                [Global hideProgressHUDInternal:self.view];
//                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
//                [Global showTextHudSub:requestResultStr view:self.view];
//            }
//            
//        }
//        else
//        {
//            [Global hideProgressHUDInternal:self.view];
//            
//            [Global showTextHudSub:@"连接失败" view:self.view];
//        }
//        
//    }];
//    
//}
//
//- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return @"删除";
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *indexPath1= [NSIndexPath indexPathForRow:indexPath.row inSection:0];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath1];
    SelectTag_NewTableViewCell *selectCell=( SelectTag_NewTableViewCell *)cell;
//     commdity.gengxin=@"";
    
    if (![selectTagCodeArray containsObject:[tagCodeArray objectAtIndex:indexPath.row]]) {
        
        [selectCell.checkButton setImage:[UIImage imageNamed:@"gous.png"] forState:UIControlStateNormal];
        
        [selectTagCodeArray addObject:[tagCodeArray objectAtIndex:indexPath.row]];
        //         [selectTagNameArray addObject:[tagNameArray objectAtIndex:indexPath.row]];
    }
    else
    {
        [selectTagCodeArray removeObject:[tagCodeArray objectAtIndex:indexPath.row]];
        //        [selectTagNameArray removeObject:[tagNameArray objectAtIndex:indexPath.row]];
        [selectCell.checkButton setImage:[UIImage imageNamed:@"quans.png"] forState:UIControlStateNormal];
    }
    
    _countLabel.text=[NSString stringWithFormat:@"已选 %lu 个",(unsigned long)selectTagCodeArray.count];
    
//    if (selectTagCodeArray.count==0) {
//        [_countView setBackgroundColor:[UIColor colorWithRed:225.0f/255.0f green:225.0f/255.0f blue:225.0f/255.0f alpha:1.0]];
//    }
//    else
//    {
//        [_countView setBackgroundColor:[UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.8]];
//    }
    
}
- (IBAction)confirmBtnClick:(id)sender {
    NSMutableArray *selectTagNameArray=[NSMutableArray array];
    for (int i=0; i<tagNameArray.count; i++) {
        for (int j=0; j<selectTagCodeArray.count; j++) {
            if ([[tagCodeArray objectAtIndex:i] isEqualToString:[selectTagCodeArray objectAtIndex:j]]) {
                [selectTagNameArray addObject:[tagNameArray objectAtIndex:i]];
                
            }
        }
    }
//    
//    commdity.gengxin=@"";
//    commdity.selectTagCodeArray=[selectTagCodeArray copy];
//    
//    commdity.selectTagNameArray=[selectTagNameArray copy];
    //        NSLog(@"------%@",selectTagCodeArray);
    //     NSLog(@"------%@",selectTagNameArray);
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setNavigation
{
    
       UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
//    UIButton*addBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0, 30, 30)];
//    [addBtn setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
//    [addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem* addBtnItem = [[UIBarButtonItem alloc] initWithCustomView:addBtn];
//    
//    self.navigationItem.rightBarButtonItem = addBtnItem;
    
    UILabel* titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,40, 23)];
    titleLabel.text=@"选择标签";
//    titleLabel.font=[UIFont systemFontOfSize: 19];
    self.navigationItem.titleView=titleLabel;
}
-(void)BackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)addBtnClick
{
    AddTagViewController *addTagVC=[[AddTagViewController alloc] init];
    addTagVC.optionNameStr=self.optionNameString;
    [self.navigationController pushViewController:addTagVC animated:YES];
}
-(void)setAutolayout
{
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(_bottomView.mas_top);
        make.top.equalTo(self.view.mas_top);
    }];
    
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@50) ;
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(_bottomView.mas_right) ;
        make.width.equalTo(@140) ;
        make.height.equalTo(@50) ;
        make.bottom.equalTo(_bottomView.mas_bottom);
    }];
    [_countView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bottomView.mas_left) ;
        make.right.equalTo(_confirmBtn.mas_left) ;
        make.height.equalTo(@50) ;
        make.bottom.equalTo(_bottomView.mas_bottom);
    }];
    
    
}

@end
